<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom:10px">
      <el-date-picker
        v-model="searchTime"
        type="date"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        placeholder="选择日期时间"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="fetchData()">搜索</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="openAdd"
      >新建</el-button>
    </div>
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">{{ scope.$index }}</template>
      </el-table-column>
      <el-table-column label="ID" align="center">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="工作ID" align="center">
        <template slot-scope="scope">{{ scope.row.workId }}</template>
      </el-table-column>
      <el-table-column label="周次" align="center">
        <template slot-scope="scope">{{ scope.row.weekly }}</template>
      </el-table-column>
      <el-table-column label="工作时间" align="center" width="145">
        <template slot-scope="scope">{{ scope.row.workTime }}</template>
      </el-table-column>
      <el-table-column label="星期" align="center">
        <template slot-scope="scope">{{ scope.row.week }}</template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">{{ scope.row.userName }}</template>
      </el-table-column>
      <el-table-column label="工作内容" align="center">
        <template slot-scope="scope">{{ scope.row.workContent }}</template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="155">
        <template slot-scope="scope">{{ scope.row.createdTime }}</template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="175" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="openEdit(row.id)">
            修改
          </el-button>
          <el-button size="mini" type="danger" @click="isDelete(row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display:flex">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper, ->, total, slot"
        :total="total"
        @current-change="currentChange"
        @size-change="sizeChange"
      />
    </div></div>
</template>

<script>

export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 10,
      searchTime: ''
    }
  },
  // 启动自动执行该方法
  created() {
    this.fetchData()
  },
  methods: {
    search() { // 日期查询
      this.$axios.get('work/getWorkListByTime?page=' + this.page + '&size=' + this.size + '&time=' + this.searchTime)
        .then(function(res) {
          this.list = res.data
          this.total = res.total
        })
      // const { data: res } = this.$axios.get('work/getWorkListByTime?page=' + this.page + '&size=' + this.size + '&time=' + this.searchTime)
      // console.log(res)
      // this.list = res.data
      // this.total = res.total
    },
    openAdd() {
      this.$router.push('/work/addWork')
    },
    openEdit(id) {
      this.$router.push({ name: 'EditWork', params: { id: id }})
    },
    sizeChange(pageSize) {
      this.size = pageSize
      this.fetchData()
    },
    currentChange(currentPage) {
      this.page = currentPage
      this.fetchData()
    },
    async fetchData() {
      if (this.searchTime === '' || this.searchTime === null) {
        const { data: res } = await this.$axios.get('work/getWorkList?page=' + this.page + '&size=' + this.size)
        this.list = res.data
        this.total = res.total
      } else { // 日期查询
        const { data: res } = await this.$axios.get('work/getWorkListByTime?page=' + this.page + '&size=' + this.size + '&time=' + this.searchTime)
        this.list = res.data
        this.total = res.total
      }
      this.$message({
        type: 'success',
        message: '查询成功!'
      })
    },
    getListByDate() {
      const { data: res } = this.$axios.get('work/getWorkList?page=' + this.page + '&size=' + this.size)
      this.list = res.data
      this.total = res.total
    },
    isDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delete(id) {
      this.$axios.delete('work/deleteWork?id=' + id)
        .then(res => {
          this.fetchData()
        })
    }
  }
}
</script>
